//
//  OnlineQuotes.m
//  HappyStore
//
//  Created by 李小辉 on 16/11/23.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "OnlineQuotes.h"
#import "QnlineQuotesVIew.h"
#import "datePickView.h"
@interface OnlineQuotes ()<selectedDateDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) QnlineQuotesVIew *qnlinQuoteView;

@end

@implementation OnlineQuotes

- (QnlineQuotesVIew *)qnlinQuoteView{
    if (!_qnlinQuoteView) {
        _qnlinQuoteView = [[[NSBundle mainBundle] loadNibNamed:@"QnlineQuotesVIew" owner:self options:nil] lastObject];
    }
    return _qnlinQuoteView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏title的字体大小等
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19],NSFontAttributeName,RGB(45, 45, 45),NSForegroundColorAttributeName, nil]];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    rightItem.tintColor = RGB(45, 45, 45);
    self.navigationItem.rightBarButtonItem = rightItem;
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.qnlinQuoteView.msgTextView.layer.borderWidth = 1.0f;
    self.qnlinQuoteView.msgTextView.layer.cornerRadius = 5.0f;
    self.qnlinQuoteView.msgTextView.layer.masksToBounds = YES;
    self.qnlinQuoteView.msgTextView.layer.borderColor = RGB(235, 235, 235).CGColor;
    //通知:监听文字的改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self.qnlinQuoteView.msgTextView];
    //在线订购or在线报价
    if ([self.typeStr integerValue] == 1) {
        self.title = @"在线订购";
        self.qnlinQuoteView.purchaseTitleLabel.text = @"可供应数量：";
        self.qnlinQuoteView.stopTimeBtn.hidden = NO;
        self.qnlinQuoteView.quantityTextField.placeholder = @"选择截止时间";
        self.qnlinQuoteView.priceTextField.placeholder = @"输入订购数量";
        [self.qnlinQuoteView.stopTimeBtn addTarget:self action:@selector(timeClickBtn) forControlEvents:UIControlEventTouchUpInside];
    }else{
        self.title = @"在线报价";
    }
    [self.qnlinQuoteView setFrame:CGRectMake(0, 0, self.qnlinQuoteView.width, self.qnlinQuoteView.height)];
    [self.scrollView addSubview:self.qnlinQuoteView];
    self.scrollView.contentSize = CGSizeMake(screenW, self.qnlinQuoteView.height);
//基础信息
    self.qnlinQuoteView.industryLabel.text = self.dataDic[@"industry"];
    self.qnlinQuoteView.adressLabel.text = self.dataDic[@"address"];
    self.qnlinQuoteView.purchasePriceLabel.text = self.dataDic[@"money"];
    self.qnlinQuoteView.purchaseQuantityLabel.text = self.dataDic[@"number"];
    self.qnlinQuoteView.unitLabel.text = self.dataDic[@"unit"];
    self.qnlinQuoteView.stopTime.text = self.dataDic[@"endTime"];

}
- (void)textDidChange{
    UILabel *label = [self.qnlinQuoteView viewWithTag:30];
    if (self.qnlinQuoteView.msgTextView.text.length == 0) {
        label.hidden = NO;
    }else{
        label.hidden = YES;
    }
}
-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)timeClickBtn{
    datePickView *dateee = [datePickView sharedSelectedAddress];
    dateee.delegatee = self;
    [dateee btnClickDate];
}
- (void)seceltedDate:(NSString *)date{
    self.qnlinQuoteView.quantityTextField.text = [date substringToIndex:10];
}
#pragma mark - 发送
- (void)rightAction{
//http://192.168.1.188:8080/lensum/addSupply_app_chain.im.lensum?token=EhnDuxqM6n8u9OIIuwcT&type=1&userId=10079&supplyId=1&offerMoney=10&offerNumner=100&contactName=xm&contactPhone=13245678754&offerRemark=beizhu
//    参数说明：type=类型 1-供应 2-采购&userId=10079&supplyId=采购供应ID&offerMoney=报价的钱&offerNumner=数量&contactName=联系人&contactPhone=联系电话&offerRemark=备注       files-为文件
//    4.报价
//http://192.168.1.188:8080/lensum/offerSupply_app_chain.im.lensum?token=EhnDuxqM6n8u9OIIuwcT&type=1&userId=1&supplyId=1&offerMoney=10&offerNumner=100&contactName=xm&contactPhone=13245678754&offerRemark=beizhu
//    参数说明：type=类型 1-供应 2-采购&userId=10079&supplyId=采购供应ID&offerMoney=报价的钱&offerNumner=数量&contactName=联系人&contactPhone=联系电话&offerRemark=备注       files-为文件
    if ([USER_DEFAULTS objectForKey:@"userid"]!=nil) {
        
        BOOL isOK = YES;
        
        if (isOK && self.qnlinQuoteView.priceTextField.text.length == 0) {
            isOK = NO;
            [self showSingleIconBubble:@"请完善信息"];
            
        }
        if (isOK && self.qnlinQuoteView.quantityTextField.text.length == 0) {
            isOK = NO;
             [self showSingleIconBubble:@"请完善信息"];
            
        }
        if (isOK && self.qnlinQuoteView.msgTextView.text.length == 0) {
            isOK = NO;
             [self showSingleIconBubble:@"请完善信息"];
        }
        if ([self.typeStr integerValue] == 1) {//供应
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            NSDate *nowTime = [NSDate date];
            //        nowTime = [dateFormatter dateFromString:];
            NSDate *endTime = [dateFormatter dateFromString:self.qnlinQuoteView.quantityTextField.text];
            
            NSComparisonResult *result = [nowTime compare:endTime];
            if (isOK && result != NSOrderedAscending) {
                isOK = NO;
                 [self showSingleIconBubble:@"截止时间已过"];
            }
        }
        if (isOK) {
            NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
            muDic[@"type"] = self.typeStr;
            
            muDic[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
            muDic[@"token"] = [USER_DEFAULTS objectForKey:@"token"];
            
            muDic[@"supplyId"] = self.dataDic[@"id"];
            
            if ([self.typeStr integerValue] == 1) {//供应
                muDic[@"offerMoney"] = self.qnlinQuoteView.quantityTextField.text;
                muDic[@"offerNumner"] = self.qnlinQuoteView.priceTextField.text;
            }else{
                muDic[@"offerMoney"] = self.qnlinQuoteView.priceTextField.text;
                muDic[@"offerNumner"] = self.qnlinQuoteView.quantityTextField.text;
            }
            
            muDic[@"offerRemark"] = self.qnlinQuoteView.msgTextView.text;
            
            //＝＝＝＝＝＝＝＝
            muDic[@"contactName"] = @"";
            muDic[@"contactPhone"] = @"";

            NSString *urlStr = @"offerSupply_app_chain.im.lensum";
            [LSHttpTool sendHttpData:urlStr params:muDic success:^(id responseObject) {
                NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];


                [self showRightWithTitle:dicInfo[@"msg"] autoCloseTime:2];
                [self.navigationController popViewControllerAnimated:YES];

//                if ([dicInfo[@"result"] integerValue] == 1) {
//                    [MRProgressOverlayView showSuccessWithStatus:dicInfo[@"msg"]];
//                    [self.navigationController popViewControllerAnimated:YES];
//                }else{
//                    [MRProgressOverlayView showSuccessWithStatus:dicInfo[@"msg"]];
//                }

                
            } failure:^(NSError *error) {

                [self showSingleIconBubble:@"未搜索到资源"];
            }];
            
        }
    }else{
        //登录
        UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *loginNAV = [LoginAndRegister instantiateViewControllerWithIdentifier:@"LoginNav"];
        [self presentViewController:loginNAV animated:YES completion:nil];
    }
    
}
//别忘记销毁
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
}

@end
